package cn.zcscloud.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.zcscloud.mall.mbg.mapper.UmsAdminMapper;
import cn.zcscloud.mall.mbg.model.*;
import cn.zcscloud.mall.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UmsAdminServiceImpl extends ServiceImpl<UmsAdminMapper, UmsAdmin> implements UmsAdminService {

    @Autowired
    private UmsAdminRoleRelationService umsAdminRoleRelationService;

    @Autowired
    private UmsRoleService umsRoleService;

    @Autowired
    private UmsAdminLoginLogService umsAdminLoginLogService;

    @Autowired
    private UmsAdminPermissionRelationService umsAdminPermissionRelationService;

    /**
     * 获取指定用户的角色
     *
     * @param adminId
     * @return
     */
    @Override
    public List<UmsRole> role(Integer adminId) {
        LambdaQueryWrapper<UmsAdminRoleRelation> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(UmsAdminRoleRelation::getAdminId, adminId);
        List<UmsAdminRoleRelation> list = umsAdminRoleRelationService.list(lqw1);
        List<Long> roleIds = CollUtil.getFieldValues(list, "roleId", Long.class);
        if (CollUtil.isNotEmpty(roleIds)) {
            return umsRoleService.listByIds(roleIds);
        }
        return null;
    }

    /**
     * 给用户分配角色
     *
     * @param adminId
     * @param roleIds
     * @return
     */
    @Override
    public void updateRole(Integer adminId, List<Integer> roleIds) {
        //先删除
        LambdaQueryWrapper<UmsAdminRoleRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsAdminRoleRelation::getAdminId, adminId);
        umsAdminRoleRelationService.remove(lqw);
        //在设置
        if (CollUtil.isNotEmpty(roleIds)) {
            for (Integer roleId : roleIds) {
                UmsAdminRoleRelation umsAdminRoleRelation = new UmsAdminRoleRelation();
                umsAdminRoleRelation.setRoleId(roleId.longValue());
                umsAdminRoleRelation.setAdminId(adminId.longValue());
                umsAdminRoleRelationService.save(umsAdminRoleRelation);
            }
        }
    }

    /**
     * 根据id删除指定用户信息
     *
     * @param id
     * @return
     */
    @Override
    public void deleteById(Integer id) {
        //删除登录日志表
        LambdaQueryWrapper<UmsAdminLoginLog> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(UmsAdminLoginLog::getAdminId, id);
        umsAdminLoginLogService.remove(lqw1);
        //删除用户和权限(permission)的关系
        LambdaQueryWrapper<UmsAdminPermissionRelation> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(UmsAdminPermissionRelation::getAdminId, id);
        umsAdminPermissionRelationService.remove(lqw2);
        //删除用户和角色的关系
        LambdaQueryWrapper<UmsAdminRoleRelation> lqw3 = new LambdaQueryWrapper<>();
        lqw3.eq(UmsAdminRoleRelation::getAdminId, id);
        umsAdminRoleRelationService.remove(lqw3);
        //再删除自身表的数据
        this.removeById(id);
    }
}
